<template>
	<view>
		<view style="padding: 0 30rpx;" >
			<u-form :model="form" ref="uForm" label-width="auto">
				<!-- 上传图片 -->
				<u-form-item label="驾驶证图片">
					<u-upload ref="uUpload" :auto-upload="false" :show-progress="true" @on-list-change="onListChange"
						:max-count="1" :form-data="{path:'/static/img/license_front_img/'}" :multiple="false">
					</u-upload>
				</u-form-item>
				<u-form-item label="驾驶员手机号" prop="driver_tel">
					<u-input placeholder="请输入驾驶员手机号" v-model="form.driver_tel" type="tel" />
				</u-form-item>
				<u-form-item label="驾驶员姓名" prop="driver_name">
					<u-input placeholder="请输入驾驶员姓名" v-model="form.driver_name" />
				</u-form-item>
				<u-form-item label="驾照号码" prop="license_num">
					<u-input placeholder="请输入驾照号码" v-model="form.license_num" />
				</u-form-item>
				<u-form-item label="准驾车型" prop="license_permit_name">
					<u-input placeholder="请输入准驾车型" v-model="form.license_permit_name" />
				</u-form-item>
				<u-form-item label="驾照有效期" prop="license_expire_date">
					<u-input placeholder="请输入驾照有效期" v-model="form.license_expire_date" />
				</u-form-item>
				<u-form-item label="初次申领日期" prop="license_first_date">
					<u-input placeholder="请输入初次申领日期" v-model="form.license_first_date" />
				</u-form-item>
				<u-form-item label="驾驶年龄" prop="driver_yaer">
					<u-input placeholder="请输入驾驶年龄" v-model="form.driver_yaer" />
				</u-form-item>
				<u-form-item label="族别" prop="driver_nation">
					<u-input placeholder="请输入族别" v-model="form.driver_nation" />
				</u-form-item>
				<u-form-item label="年龄" prop="driver_age">
					<u-input placeholder="请输入年龄" v-model="form.driver_age" />
				</u-form-item>
				<u-form-item label="性别" prop="driver_gender">
					<u-radio-group v-model="form.driver_gender">
						<u-radio v-for="(item, index) in [{value:1,name:'男'},{value:2,name:'女'}]" :key="index"
							:name="item.value">
							{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="驾驶员住址" prop="driver_address">
					<u-input placeholder="请输入驾驶员住址" v-model="form.driver_address" />
				</u-form-item>
				<u-form-item label="备注" prop="remark">
					<u-input placeholder="请输入备注" v-model="form.remark" />
				</u-form-item>

				<u-form-item label="紧急联系电话" prop="driver_contact_tel">
					<u-input placeholder="请输入紧急联系电话" v-model="form.driver_contact_tel" />
				</u-form-item>

				<u-form-item>
					<u-button type="primary" @click="submit">添加驾驶员</u-button>
				</u-form-item>
			</u-form>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import cos from '../../utils/cos';
	import dateUtils from '../../utils/dateUtils';
	export default {
		data() {
			return {
				orders:null,
				form: {
					license_front_img: '',
					driver_name: '',
					license_num: '',
					license_permit_name: '',
					license_expire_date: '',
					license_first_date: '',
					driver_yaer: '',
					driver_nation: '',
					driver_age: '',
					driver_gender: '',
					driver_address: '',
					remark: '',
					driver_tel: '',
					driver_contact_tel: ''
				},
				rules: {
					driver_name: [{
						required: true,
						message: '请输入驾驶员姓名',
						trigger: ['blur', 'change']
					}],
					driver_tel: [{
						required: true,
						message: '请输入驾驶员手机号',
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		onLoad(options) {
			console.log(options);
			if (options.data) {
				this.orders = JSON.parse(decodeURIComponent(options.data));
				console.log(this.orders);
			}
		},
		methods: {
			submit() {
				var that = this;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						var that = this;
						that.form.orders_id = that.orders.orders_id;
						that.$http.post('/orderDriver/insert', that.form, (res) => {
							if (res.data.success) {
								that.$refs.uToast.show({
									title: '添加成功',
									type: "success"
								});
								uni.$emit("getOrdersInfo", that.data);
								uni.navigateBack();
							} else {
								that.refs.myModal.show('请求失败', res.data.result.message);
							}
						});
					} else {
						console.log('验证失败');
					}
				});
			},
			onListChange() {
				var that = this;
				var list = this.$refs.uUpload.lists;
				var dir = '/static/img/license_front_img/';
				cos.uploadFiles(list, dir, (data) => {
					console.log(data);
					var options = data.files[0].options;
					var imgUrl = 'https://cos2.ksmeilv.com' + options.Key;
					var that = this;
					that.$http.post('/vehicleLicense/OCR_DriverLicense', {
						url: imgUrl
					}, (res) => {
						if (res.data.success) {
							that.form = res.data.result;
							that.form.driver_yaer = dateUtils.timestampToYear(that.form
								.license_first_date);
							that.form.driver_age = dateUtils.getIdCardAge(that.form.license_num);
							that.form.license_front_img = options.Key;
							that.form.driver_birthday=that.$u.timeFormat(that.form.driver_birthday, 'yyyy-mm-dd');
							that.form.license_date=that.$u.timeFormat(that.form.license_date, 'yyyy-mm-dd')
							that.form.license_expire_date=that.$u.timeFormat(that.form.license_expire_date, 'yyyy-mm-dd')
							that.form.license_first_date=that.$u.timeFormat(that.form.license_first_date, 'yyyy-mm-dd')
							that.form.driver_gender=res.data.result.driver_gender=='男'?1:2;
						} else {
							that.refs.myModal.show('请求失败', res.data.result.message);
						}
					});
				});
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>

</style>